package io.bba.boot.app.controller;

import io.bba.boot.auth.annotation.Permission;
import io.bba.boot.common.enums.SysLogType;
import io.bba.boot.framework.annotation.Log;
import io.bba.boot.framework.page.Paging;
import io.bba.boot.framework.response.ApiResult;
import io.bba.boot.app.dto.CrawlerUrlDto;
import io.bba.boot.app.query.CrawlerUrlQuery;
import io.bba.boot.app.service.CrawlerUrlService;
import io.bba.boot.app.vo.CrawlerUrlVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 采集记录 控制器
 *
 * @author zs
 * @since 2024-03-19
 */
@Slf4j
@RestController
@Tag(name = "采集记录")
@RequestMapping("/admin/crawlerUrl")
public class CrawlerUrlController {

    @Autowired
    private CrawlerUrlService crawlerUrlService;

    /**
     * 添加采集记录
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加采集记录")
    @PostMapping("/addCrawlerUrl")
    @Permission("crawler:url:add")
    public ApiResult addCrawlerUrl(@Valid @RequestBody CrawlerUrlDto dto) {
        log.info("添加采集记录：{}", dto);
        boolean flag = crawlerUrlService.addCrawlerUrl(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改采集记录
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改采集记录")
    @PostMapping("/updateCrawlerUrl")
    @Permission("crawler:url:update")
    public ApiResult updateCrawlerUrl(@Valid @RequestBody CrawlerUrlDto dto) {
        log.info("修改采集记录：{}", dto);
        boolean flag = crawlerUrlService.updateCrawlerUrl(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除采集记录
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除采集记录")
    @PostMapping("/deleteCrawlerUrl/{id}")
    @Permission("crawler:url:delete")
    public ApiResult deleteCrawlerUrl(@PathVariable Long id) {
        log.info("删除采集记录：{}", id);
        boolean flag = crawlerUrlService.deleteCrawlerUrl(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取采集记录详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取采集记录详情")
    @PostMapping("/getCrawlerUrl/{id}")
    @Permission("crawler:url:info")
    public ApiResult<CrawlerUrlVo> getCrawlerUrl(@PathVariable Long id) {
        log.info("获取采集记录详情：{}", id);
        CrawlerUrlVo crawlerUrlVo = crawlerUrlService.getCrawlerUrlById(id);
        return ApiResult.success(crawlerUrlVo);
    }

    /**
     * 获取采集记录分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取采集记录分页列表")
    @PostMapping("/getCrawlerUrlPage")
    @Permission("crawler:url:page")
    public ApiResult<CrawlerUrlVo> getCrawlerUrlPage(@Valid @RequestBody CrawlerUrlQuery query) {
        log.info("获取采集记录分页列表：{}", query);
        Paging<CrawlerUrlVo> paging = crawlerUrlService.getCrawlerUrlPage(query);
        return ApiResult.success(paging);
    }

}
